//SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

use proto_gen_rust::ai_provider_api::ai_provider_admin_api_client::AiProviderAdminApiClient;
use proto_gen_rust::ai_provider_api::*;
use tauri::{AppHandle, Emitter, Runtime};

use crate::notice_decode::new_wrong_session_notice;

#[tauri::command]
pub async fn ai_provider_admin_api_cmds_add_llm_provider<R: Runtime>(
    app_handle: AppHandle<R>,
    request: AdminAddLlmProviderRequest,
) -> Result<AdminAddLlmProviderResponse, String> {
    let chan = crate::get_grpc_chan(&app_handle).await;
    if (&chan).is_none() {
        return Err("no grpc conn".into());
    }
    let mut client = AiProviderAdminApiClient::new(chan.unwrap());
    match client.add_llm_provider(request).await {
        Ok(response) => {
            let inner_resp = response.into_inner();
            if inner_resp.code == admin_add_llm_provider_response::Code::WrongSession as i32
                || inner_resp.code == admin_add_llm_provider_response::Code::NotAuth as i32
            {
                crate::admin_auth_api_cmds::logout(app_handle.clone()).await;
                if let Err(err) = app_handle.emit(
                    "notice",
                    new_wrong_session_notice("add_llm_provider".into()),
                ) {
                    println!("{:?}", err);
                }
            }
            return Ok(inner_resp);
        }
        Err(status) => Err(status.message().into()),
    }
}

#[tauri::command]
pub async fn ai_provider_admin_api_cmds_update_llm_provider<R: Runtime>(
    app_handle: AppHandle<R>,
    request: AdminUpdateLlmProviderRequest,
) -> Result<AdminUpdateLlmProviderResponse, String> {
    let chan = crate::get_grpc_chan(&app_handle).await;
    if (&chan).is_none() {
        return Err("no grpc conn".into());
    }
    let mut client = AiProviderAdminApiClient::new(chan.unwrap());
    match client.update_llm_provider(request).await {
        Ok(response) => {
            let inner_resp = response.into_inner();
            if inner_resp.code == admin_update_llm_provider_response::Code::WrongSession as i32
                || inner_resp.code == admin_update_llm_provider_response::Code::NotAuth as i32
            {
                crate::admin_auth_api_cmds::logout(app_handle.clone()).await;
                if let Err(err) = app_handle.emit(
                    "notice",
                    new_wrong_session_notice("update_llm_provider".into()),
                ) {
                    println!("{:?}", err);
                }
            }
            return Ok(inner_resp);
        }
        Err(status) => Err(status.message().into()),
    }
}


#[tauri::command]
pub async fn ai_provider_admin_api_cmds_remove_llm_provider<R: Runtime>(
    app_handle: AppHandle<R>,
    request: AdminRemoveLlmProviderRequest,
) -> Result<AdminRemoveLlmProviderResponse, String> {
    let chan = crate::get_grpc_chan(&app_handle).await;
    if (&chan).is_none() {
        return Err("no grpc conn".into());
    }
    let mut client = AiProviderAdminApiClient::new(chan.unwrap());
    match client.remove_llm_provider(request).await {
        Ok(response) => {
            let inner_resp = response.into_inner();
            if inner_resp.code == admin_remove_llm_provider_response::Code::WrongSession as i32
                || inner_resp.code == admin_remove_llm_provider_response::Code::NotAuth as i32
            {
                crate::admin_auth_api_cmds::logout(app_handle.clone()).await;
                if let Err(err) = app_handle.emit(
                    "notice",
                    new_wrong_session_notice("remove_llm_provider".into()),
                ) {
                    println!("{:?}", err);
                }
            }
            return Ok(inner_resp);
        }
        Err(status) => Err(status.message().into()),
    }
}